package app.GUI;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import app.GUI.LogIn.CancelHandler;
import app.GUI.LogIn.CreateAccountHandler;
import app.GUI.LogIn.LogInHandler;

import app.Data.*;
import app.Logic.*;

public class CreateAccount{
	private static HomeScreen outsideFrame;
	
	private static JFrame frame;
	private static UserAccounts accounts;
	
	private static JTextField userNameTextField;
	private static JTextField passwordTextField;
	private static JTextField password2TextField;
	
	private static JButton createAccountButton;
	private static JButton cancelButton;
	
	private static CreateAccountHandler createAccountButtonHandler;
	private static CancelHandler cancelButtonHandler;
	
	public CreateAccount(UserAccounts list, HomeScreen outside){
		outsideFrame = outside;
		
		frame = new JFrame();
		frame.setSize(350, 175);
		frame.setResizable(false);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		
		accounts = list;
		createAccountButtonHandler = new CreateAccountHandler();
		cancelButtonHandler = new CancelHandler();	
	}
	
	public void createAccountPanel(){
		JPanel innerPanel = new JPanel();
		innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.Y_AXIS));


		JPanel userN = new JPanel();
		JLabel nameLabel = new JLabel("            User Name: ");
		userNameTextField = new JTextField(10);
		userN.add(nameLabel);
		userN.add(userNameTextField);
		innerPanel.add(userN);
		
		JPanel passwordN = new JPanel();
		JLabel passwordLabel = new JLabel("             Password: ");
		passwordTextField = new JTextField(10);
		passwordN.add(passwordLabel);
		passwordN.add(passwordTextField);
		innerPanel.add(passwordN);
		
		JPanel passwordN2 = new JPanel();
		JLabel passwordLabel2 = new JLabel("Re-Enter Password:");
		password2TextField = new JTextField(10);
		passwordN2.add(passwordLabel2);
		passwordN2.add(password2TextField);
		innerPanel.add(passwordN2);
		
		createAccountButton = new JButton("Create Account");
		cancelButton = new JButton("Cancel");
		
		createAccountButton.addActionListener(createAccountButtonHandler);
		cancelButton.addActionListener(cancelButtonHandler);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(createAccountButton);
		buttonPanel.add(cancelButton);
		innerPanel.add(buttonPanel);
		
		frame.add(innerPanel);
		frame.setVisible(true);
		
	}

	class CreateAccountHandler implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String userName = userNameTextField.getText();
			String password = passwordTextField.getText();
			String password2 = password2TextField.getText();
			
			if(!(password2.equals(password))){
				JOptionPane.showMessageDialog(frame, "The passwords do not match!", 
						"Account Creation Error", 
						JOptionPane.ERROR_MESSAGE);
			}
			else if((userName.equals("")) || (password.equals("")) || (password2.equals(""))){
				JOptionPane.showMessageDialog(frame, "User name and/or password is not valid", 
						"Account Creation Error", 
						JOptionPane.ERROR_MESSAGE);
			}
			else if(accounts.checkUserName(userName)){
				JOptionPane.showMessageDialog(frame, "User account already exists!", 
						"Account Creation Error", 
						JOptionPane.ERROR_MESSAGE);
			}
			else{
				accounts.addUser(userName, password);
			
				JOptionPane.showMessageDialog(frame, 
						"The following account has been created:\n" +
						"\nUserName: "+userName+
						"\nPassword: "+password, 
						"Create Account", 
						JOptionPane.INFORMATION_MESSAGE);
				
				frame.setVisible(false);
				frame.removeAll();
			}
		}
	}
	
	class CancelHandler implements ActionListener{
		public void actionPerformed(ActionEvent e){
			frame.setVisible(false);
			frame.removeAll();
			
		}
	}
}
